<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/9/3 15:17
 */

namespace App\Service\User;


use App\Constants\System\SysStatusConst;
use App\Constants\User\UserAccountDefaultConst;
use App\Dao\Users\UsersAccountDao;
use App\Exception\ServiceException;
use App\Model\Users\UsersAccount;
use App\Utils\AmountUtils;
use Hyperf\Di\Annotation\Inject;

class AccountService
{


    #[Inject]
    protected UsersAccountDao $usersAccountDao;


    public function accountList()
    {
        $userInfo = auth()->user();

        $userAccount = UsersAccount::query()
            ->select(['id','account_name','remark','created_at','is_default','updated_at','type','amount','in_amount','out_amount'])
            ->where('uid','=',$userInfo->getId())->where('is_del','=',SysStatusConst::NORMAL->value)
            ->limit(100)->orderBy('is_default','desc')->orderBy('id','desc');

        $userAccountData = $userAccount->get()->toArray();
        $totalInAmount = $totalOutAmount = $totalAmount = 0;
        foreach ($userAccountData as &$item)
        {
            $totalInAmount += $item['in_amount'];
            $totalOutAmount += $item['out_amount'];
            $totalAmount += $item['amount'];
            $item['in_amount'] = AmountUtils::fenToYuan($item['in_amount']);
            $item['out_amount'] = AmountUtils::fenToYuan($item['out_amount']);
            $item['amount'] = AmountUtils::fenToYuan($item['amount']);
        }
        $allAccountItem = [
            'account_name' => '所有账册',
            'id' => 0,
            'amount' => AmountUtils::fenToYuan($totalAmount),
            'in_amount' => AmountUtils::fenToYuan($totalInAmount),
            'out_amount' => AmountUtils::fenToYuan($totalOutAmount),
            'type' => 0,
            'remark' => '所有账册汇总',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        array_unshift($userAccountData,$allAccountItem);


        return $userAccountData;
    }


    public function accountSave(array $data)
    {
        $userInfo = auth()->user();
        if(!empty($data['id']))
        {
            $userAccountInfo = UsersAccount::query()
                ->where('id','=',$data['id'])
                ->where('uid','=',$userInfo->getId())->first();
            unset($data['amount']);
        }else{
            $data['uid'] =  $userInfo->getId();
            $userAccountInfo = new UsersAccount();
        }

        $res = $userAccountInfo->fill($data)->save();
        if(!$res) throw new ServiceException("保存失败");
        /**
         * 排除已设置默认的其他均设置为非默认
         */
        if($data['is_default'] == UserAccountDefaultConst::DEFAULT->value)
        {
            $this->usersAccountDao->updateAccountNotDefault($userInfo->getId(),$userAccountInfo->id);
        }
        return $userAccountInfo->id;
    }



    public function accountRemove(int $id)
    {
        $userInfo = auth()->user();
        $userAccountInfo = UsersAccount::query()
            ->where('id','=',$id)
            ->where('uid','=',$userInfo->getId())->first();

        if(!$userAccountInfo) throw new ServiceException("账册不存在");

        $userAccountInfo->is_del = SysStatusConst::DELETE->value;

        $res = $userAccountInfo->save();

        if (!$res) throw new ServiceException("删除失败");
    }
}